<template>
  <div class="ma-content-block lg:flex justify-between p-4">
    <!-- CRUD 组件 -->
    <ma-crud :options="options" :columns="columns" ref="crudRef">
      <template #bind_corp_id="{ record }">{{ record.bind_corp_id?'已绑定':'未绑定'}}</template>
    </ma-crud>
  </div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import userDevice from '@/api/backend/device.js'
import { Message } from '@arco-design/web-vue'
import tool from '@/utils/tool.js'
import * as common from '@/utils/common.js'

const crudRef = ref()



const switchStatus = (statusValue, id, statusName) => {
  userDevice.changeStatus({ id, statusName, statusValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}


const options = reactive({
  id: 'user_device',
  rowSelection: {
    showCheckedAll: true
  },
  pk: 'id',
  operationColumn: true,
  operationColumnWidth: 160,
  formOption: {
    viewType: 'drawer',
    width: 600
  },
  api: userDevice.getList,
  recycleApi: userDevice.getRecycleList,
  add: {
    show: true,
    api: userDevice.save,
    auth: ['device:save']
  },
  edit: {
    show: true,
    api: userDevice.update,
    auth: ['device:update']
  },
  delete: {
    show: true,
    api: userDevice.deletes,
    auth: ['device:delete'],
    realApi: userDevice.realDeletes,
    realAuth: ['device:realDeletes']
  },
  recovery: {
    show: true,
    api: userDevice.recoverys,
    auth: ['device:recovery']
  }
})

const columns = reactive([
  {
    title: "ID",
    dataIndex: "id",
    formType: "input",
    addDisplay: false,
    editDisplay: false
  },
  {
    title: "创建时间",
    dataIndex: "created_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    showTime: true,
    hide: true
  },
  {
    title: "更新时间",
    dataIndex: "updated_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "删除时间",
    dataIndex: "deleted_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "设备名称",
    dataIndex: "device_name",
    formType: "input",
    search: true,
    commonRules: {required: true, message: "请输入名称"},
  },
  {
    title: "设备类型",
    dataIndex: "device_type",
    formType: "select",
    search: true,
    commonRules: {required: true, message: "请选择类型"},
    multiple: false,
    dict: {
      name: 'device_type',
      props: {label: 'title', value: 'key'},
      translation: true
    },
  },
  {
    title: "MAC地址",
    dataIndex: "mac_address",
    formType: "input",
    commonRules: {required: true, message: "请输入MAC地址"},
    search: true
  },
  {
    title: "在线",
    dataIndex: "online",
    formType: "select",
    dict: {
      data: [
        {
          label: "是",
          value: "1"
        },
        {
          label: "否",
          value: "0"
        }
      ],
      translation: true
    }
  },
  {
    title: "绑定",
    dataIndex: "bind_corp_id",
    addDisplay: false,
    editDisplay: false,
  },
  {
    title: "分组",
    dataIndex: "group_id",
    formType: "select",
    multiple: false,
    dict: {
      remote: 'backend/deviceGroup/remote',
      method: 'post',
      props: {label: 'name', value: 'id'},
      remoteOption: {
        select: ['id', 'name']
      },
      translation: true
    },
  },
  {
    title: "设备版本号",
    dataIndex: "app_version",
    formType: "input"
  },
  {
    title: "视频地址",
    dataIndex: "video_url",
    formType: "input"
  },
  {
    title: "设备ip",
    dataIndex: "device_ip",
    formType: "input"
  },
  {
    title: "服务器host",
    dataIndex: "server_host",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
  },
  {
    title: "设备状态",
    dataIndex: "status",
    formType: "select",
    dict: {
      data: [
        {
          label: "正常",
          value: "1"
        },
        {
          label: "禁用",
          value: "2"
        }
      ],
      translation: true
    }
  },
  {
    title: "算法版本号",
    dataIndex: "device_version",
    formType: "input"
  },
  {
    title: "设备厂商",
    dataIndex: "device_factory",
    formType: "input"
  },
  {
    title: "品牌名称",
    dataIndex: "brand_name",
    formType: "input"
  },
  {
    title: "ProductKey",
    dataIndex: "product_key",
    formType: "input",
    hide: true
  },
  {
    title: "ProductSecret",
    dataIndex: "product_secret",
    formType: "input",
    hide: true
  },
  {
    title: "备注",
    dataIndex: "remark",
    formType: "input",
  }
])
</script>
<script> export default { name: 'device' } </script>